package com.tuling.designpattern.memento;

/**
 * @author it-小林
 * @desc   客户端
 * @date 2021年09月18日 15:22
 */
public class Client {

    private static int index = -1;

    private static MementoCaretaker mementoCaretaker = new MementoCaretaker();

    public static void main(String args[]){
        Chessman chessman = new Chessman("车", 1, 1);
        play(chessman);
        chessman.setX(4);
        play(chessman);
        chessman.setY(5);
        play(chessman);
        undo(chessman, index);
        undo(chessman, index);
        redo(chessman, index);
        redo(chessman, index);

    }

    /**
     * 下棋，同时保存备忘录
     * @param chessman
     */
    public static void play(Chessman chessman){
        mementoCaretaker.addMemento(chessman.save());
        index++;
        chessman.show();
    }

    /**
     * 悔棋，撤销到上一个备忘录
     * @param chessman
     * @param i
     */
    public static void undo(Chessman chessman, int i){
        System.out.println("*******悔棋*********");
        index--;
        chessman.restore(mementoCaretaker.getMemento(i-1));
        chessman.show();
    }

    /**
     * 撤销悔棋，恢复到下一个备忘录
     * @param chessman
     * @param i
     */
    public static void redo(Chessman chessman, int i){
        System.out.println("*****撤销悔棋*****");
        index++;
        chessman.restore(mementoCaretaker.getMemento(i + 1));
        chessman.show();
    }
}
